﻿myApp.factory('authService', ['$http', '$q',
'localStorageService','$location', function ($http, $q, localStorageService, $location) {
    var serviceBase = '';
    var authServiceFactory = {};

    var _authentication = {
        isAuth: false,
        userName: "",
        userType :""
    };

    var _saveRegistration = function (registration) {
        _logOut();
        return $http.post(serviceBase + 'api/User/save', registration).then(function (response) {
            return response;
        });
    };

    var _login = function (loginData) {

        var data = "grant_type=password&username=" +
        loginData.userName + "&password=" + loginData.password;

        var deferred = $q.defer();

        $http.post(serviceBase + 'api/authorization/Token', data, {
            headers:
            { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response) {
            localStorageService.set('authorizationData',
            { token: response.access_token, userName: loginData.userName , userType : response.UserType });

            _authentication.isAuth = true;
            _authentication.userName = loginData.userName;
            _authentication.userType = response.UserType;

            deferred.resolve(response);

        }).error(function (err, status) {
            _logOut();
            deferred.reject(err);
        });

        return deferred.promise;
    };

    var _logOut = function () {

        localStorageService.remove('authorizationData');

        _authentication.isAuth = false;
        _authentication.userName = "";
        _authentication.userType = "";
    };

    var _fillAuthData = function () {

        var authData = localStorageService.get('authorizationData');
        if (authData) {
            _authentication.isAuth = true;
            _authentication.userName = authData.userName;
            _authentication.userType = authData.userType;
        }
    }

    authServiceFactory.saveRegistration = _saveRegistration;
    authServiceFactory.login = _login;
    authServiceFactory.logOut = _logOut;
    authServiceFactory.fillAuthData = _fillAuthData;
    authServiceFactory.authentication = _authentication;

    return authServiceFactory;
}]);

myApp.factory('authInterceptorService', ['$q', '$location',
'localStorageService', '$window', '$rootScope', function ($q, $location, localStorageService, $window, $rootScope) {

    var authInterceptorServiceFactory = {};

    var _request = function (config) {
       config.headers = config.headers || {};
        var authData = localStorageService.get('authorizationData');
        if (authData && config.url.indexOf('facebook') == -1) {
            config.headers.Authorization = 'Bearer ' + authData.token;
        }    
          

        return config;
    }

    var _responseError = function (rejection) {
        if (rejection.status === 401 || rejection.status === 405) {
            $window.alert('Session expired!');
            localStorageService.remove('authorizationData');
            $rootScope.authentication = null;
            $location.path('/Login');
        }
        return $q.reject(rejection);
    }

    authInterceptorServiceFactory.request = _request;
    authInterceptorServiceFactory.responseError = _responseError;

    return authInterceptorServiceFactory;
}]);

